package life.kenny.community.controller;

import life.kenny.community.dto.CommentDTO;
import life.kenny.community.dto.QuestionDTO;
import life.kenny.community.service.CommentService;
import life.kenny.community.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Jay
 * @date 2021/10/30 8:35 上午
 */
@Controller
public class QuestionController {

    @Autowired
    private QuestionService questionService;
    @Autowired
    private CommentService commentService;
    @GetMapping("/question/{id}")
    public String question(@PathVariable("id") Long id, Model model,
                           HttpServletRequest request) {

        if (id == null) {
            return "redirect:/";
        }
        QuestionDTO questionDTO = questionService.getByQuestionId(id);
        List<QuestionDTO> relatedQuestions = questionService.selectRelated(questionDTO);
        List<CommentDTO> commentDTOS = commentService.listByQuestionId(id);
        questionService.incView(id);
        questionDTO.setCommentCount(commentDTOS.size());
        model.addAttribute("question", questionDTO);
        model.addAttribute("relatedQuestions", relatedQuestions);
        model.addAttribute("comments", commentDTOS);
        return "question";
//        return "redirect:/question/" + id;
    }
}
